@echo off
REM * This batch is safe for files
REM * Only empty folders and subfolders in current directory
REM * will be deleted recursively
REM * Usage: cld [directory]

REM echo CD = %CD%
VERIFY OTHER 2>nul
SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION
IF ERRORLEVEL 1 echo Unable to enable extensions&goto END

set recursive=0
set PFLAG=0
if NOT "%~1" == "" (
    pushd "%~1">nul 2>nul
    if ERRORLEVEL 1 echo Bad folder...&goto END
    set PFLAG=1
)

for /f "tokens=*" %%a in ('dir /a:d /s /b') do @if NOT ERRORLEVEL 1 (
    REM echo "%%~dpnxa"
    rd "%%~dpnxa">nul 2>nul
    if EXIST "%%~dpnxa" (
        REM echo "%%~dpnxa" - delete failed!
    ) else (
        echo "%%~dpnxa" - deleted!
        set recursive=1
    )
) else (
    echo Unexpected Error ...
    goto END
)

:END
if !PFLAG! == 1 popd
if !recursive! == 1 (
    endlocal
    "%~0" "%~1"
)

